package com.zxl.working.service.impl;

import com.google.gson.Gson;
import com.zxl.working.entity.PwdVO;
import com.zxl.working.entity.admin.SysMenu;
import com.zxl.working.entity.admin.SysUser;
import com.zxl.working.entity.backend.SellerInfo;
import com.zxl.working.entity.backend.ServiceInfo;
import com.zxl.working.mapper.admin.UserMapper;
import com.zxl.working.mapper.backend.SellerMapper;
import com.zxl.working.mapper.backend.ServiceMapper;
import com.zxl.working.service.UserService;
import com.zxl.working.utils.CommUtil;
import com.zxl.working.utils.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@PropertySource(value = "classpath:application-dev.properties", ignoreResourceNotFound = true)
public class UserServiceImpl implements UserService {

    private Logger logger = LogManager.getLogger(UserServiceImpl.class);

    @Resource
    private UserMapper userMapper;
    @Resource
    private SellerMapper sellerMapper;
    @Resource
    private ServiceMapper serviceMapper;

    @Resource
    private Gson gson;
    @Resource
    private CommUtil commUtil;
    @Resource
    private RedisUtil redisUtil;


    public String login(String paramStr, HttpServletRequest session) {
    	Enumeration em = session.getSession().getAttributeNames();
   	 while(em.hasMoreElements()){
   		 session.getSession().removeAttribute(em.nextElement().toString());
   	 }
        SysUser sysUser = gson.fromJson(paramStr, SysUser.class);
        if (StringUtils.isNotEmpty(sysUser.getLoginName())) {
            SysUser loginUser = userMapper.qryUserByLoginName(sysUser.getLoginName());
            if (loginUser != null) {
                if (loginUser.getPassword().equals(sysUser.getPassword())) {
                    List<SysMenu> menuList = userMapper.qryMenus(loginUser.getUserId());
                    SellerInfo sellerInfo = sellerMapper.qrySellerInfoByLogin(loginUser.getLoginName(), loginUser.getUserId());
                    if (sellerInfo != null) {
                        session.getSession().setAttribute("loginUser", loginUser);
                        loginUser.setPassword("");
                        session.getSession().setAttribute("sellerInfo", sellerInfo);
                        session.getSession().setMaxInactiveInterval(30*60);
                        Map<String, Object> resMap = new HashMap<>();
                        resMap.put("userInfo", loginUser);
                        resMap.put("menus", menuList);
                        resMap.put("sellerInfo", sellerInfo);
                        return commUtil.returnSuccess(resMap);
                    } else {
                        return commUtil.returnStr(-1, "无对应商户，请重新登录", "");
                    }
                } else {
                    return commUtil.returnStr(-1, "密码错误", "");
                }
            } else {
                return commUtil.returnStr(-1, "用户不存在", "");
            }
        } else {
            return commUtil.returnStr(-1, "缺少参数", "");
        }
    }

    public String serviceLogin(String paramStr, HttpServletRequest session) {
    	 Enumeration em = session.getSession().getAttributeNames();
    	 while(em.hasMoreElements()){
    		 session.getSession().removeAttribute(em.nextElement().toString());
    	 }
        SysUser sysUser = gson.fromJson(paramStr, SysUser.class);
        if (StringUtils.isNotEmpty(sysUser.getLoginName())) {
            SysUser loginUser = userMapper.qryUserByLoginName(sysUser.getLoginName());
            if (loginUser != null) {
                if (loginUser.getPassword().equals(sysUser.getPassword())) {
                    List<SysMenu> menuList = userMapper.qryMenus(loginUser.getUserId());
                    ServiceInfo serviceInfo = serviceMapper.qryServiceInfo(loginUser.getUserId(), loginUser.getLoginName());
                    if (serviceInfo != null) {
                        session.getSession().setAttribute("loginUser", loginUser);
                        loginUser.setPassword("");
                        session.getSession().setAttribute("serviceInfo", serviceInfo);
                        session.getSession().setMaxInactiveInterval(30*60);
                        Map<String, Object> resMap = new HashMap<>();
                        resMap.put("userInfo", loginUser);
                        resMap.put("menus", menuList);
                        resMap.put("serviceInfo", serviceInfo);
                        return commUtil.returnSuccess(resMap);
                    } else {
                        return commUtil.returnStr(-1, "无对应服务商，请重新登录", "");
                    }
                } else {
                    return commUtil.returnStr(-1, "密码错误", "");
                }
            } else {
                return commUtil.returnStr(-1, "用户不存在", "");
            }
        } else {
            return commUtil.returnStr(-1, "缺少参数", "");
        }
    }

    @Override
    public String updatePwd(String param, HttpServletRequest request) {
        PwdVO PwdVO = gson.fromJson(param, PwdVO.class);
        SysUser sysUser = (SysUser) request.getSession().getAttribute("loginUser");
        SysUser loginUser = userMapper.qryUserByLoginName(sysUser.getLoginName());
        if (loginUser.getPassword().equals(PwdVO.getPassword())) {
            userMapper.updatePwd(sysUser.getUserId(),PwdVO.getNewPassword());
            return commUtil.returnStr(1, "修改成功", "");
        } else {
            return commUtil.returnStr(-1, "原始密码错误", "");
        }

    }
}